package com.pug.zixun.service.order;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pug.zixun.mapper.OrderMapper;
import com.pug.zixun.pojo.Order;
import com.pug.zixun.vo.OrderVo;
import com.pug.zixun.bo.OrderBo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.pug.zixun.commons.enums.ResultStatusEnum;
import com.pug.zixun.commons.ex.PugValidatorException;
import com.pug.zixun.commons.utils.fn.asserts.Vsserts;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 * OrderServiceImpl实现类
 * 创建人:yykk<br/>
 * 时间：2022-08-09 23:06:29 <br/>
 * 源码下载：前台代码 git clone https://gitee.com/kekesam/kuangstudy-pug-ui.git
 * 飞哥B站地址：后台代码 git clone https://gitee.com/kekesam/kuangstudy-pug-parent.git
 * @version 1.0.0<br/>
 *
*/
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper,Order> implements IOrderService  {

    /**
     * 查询分页&搜索订单列表
     * @param orderVo
     * @return IPage<Order>
     * 创建人:yykk
     * 创建时间：2022-08-09 23:06:29
     * @version 1.0.0
     */
    @Override
	public IPage<OrderBo> findOrderPage(OrderVo orderVo){
	    // 设置分页信息
		Page<Order> page = new Page<>(orderVo.getPageNo(),orderVo.getPageSize());
		// 设置查询条件
        LambdaQueryWrapper<Order> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // lambdaQueryWrapper.select(Order.class, column -> !column.getColumn().equals("description"));
        // 根据关键词搜索信息
         // 根据关键词搜索信息
          //查询发布的 0 未发布  1 发布
        lambdaQueryWrapper.eq(orderVo.getOrderNumber() != null ,Order::getOrdernumber,orderVo.getOrderNumber());

        lambdaQueryWrapper.eq(orderVo.getStatus() != null ,Order::getStatus,orderVo.getStatus());
        lambdaQueryWrapper.eq(orderVo.getType() != null ,Order::getType,orderVo.getType());

        // 根据关键词搜索信息

        // 根据时间排降序
        lambdaQueryWrapper.orderByDesc(Order::getCreateTime);
        // 查询分页返回
		IPage<Order> results = this.page(page,lambdaQueryWrapper);
		return tranferPageBo(results,OrderBo.class);
	}


	/**
     * 根据id查询订单列表明细信息
     * @param id
     * @return Order
     * 创建人:yykk
     * 创建时间：2022-08-09 23:06:29
     * @version 1.0.0
     */
    @Override
    public OrderBo getOrderById(Long id) {
        return tranferBo(this.getById(id),OrderBo.class);
    }


    /**
     * 保存&修改订单列表
     * @param order
     * @return Order
     * 创建人:yykk
     * 创建时间：2022-08-09 23:06:29
     * @version 1.0.0
     */
    @Override
	public OrderBo saveupdateOrder(Order order){
		boolean flag = this.saveOrUpdate(order);
		return flag ? tranferBo(order,OrderBo.class)  : null;
	}


    /**
     * 根据id删除订单列表
     * @param id
     * @return int
     * 创建人:yykk
     * 创建时间：2022-08-09 23:06:29
     * @version 1.0.0
     */
    @Override
    public int deleteOrderById(Long id) {
        Order adminRole = this.getById(id);
        if (adminRole != null) {
            Order order2 = new Order();
            order2.setId(id);
            boolean b = this.updateById(order2);
            return 1;
        }
        return 0;
    }

    /**
     * 根据id删除
     * @param ids
     * @return boolean
     * 创建人:yykk
     * 创建时间：2022-08-09 23:06:29
     * @version 1.0.0
     */
    @Override
    public boolean delBatchOrder(String ids) {
        try {
            // 1 : 把数据分割
            String[] strings = ids.split(",");
            // 2: 组装成一个List<Order>
            List<Order> orderList = Arrays.stream(strings).map(idstr -> {
                Order order = new Order();
                order.setId(new Long(idstr));
                return order;
            }).collect(Collectors.toList());
            // 3: 批量删除
            return this.updateBatchById(orderList);
        } catch (Exception ex) {
            throw new PugValidatorException(ResultStatusEnum.SERVER_DB_ERROR);
        }
    }


}
